package com.agileai.pm.module.base.handler;

import com.agileai.domain.DataParam;
import com.agileai.domain.DataRow;
import com.agileai.hotweb.controller.core.StandardEditHandler;
import com.agileai.hotweb.domain.FormSelectFactory;
import com.agileai.hotweb.renders.LocalRenderer;
import com.agileai.hotweb.renders.RedirectRenderer;
import com.agileai.hotweb.renders.ViewRenderer;
import com.agileai.pm.module.base.service.ProjectReportManage;

public class ProjectReportManageEditHandler
        extends StandardEditHandler {
    public ProjectReportManageEditHandler() {
        super();
        this.listHandlerClass = ProjectReportManageListHandler.class;
        this.serviceId = buildServiceId(ProjectReportManage.class);
    }
    
    public ViewRenderer prepareDisplay(DataParam param) {
		String operaType = param.get(OperaType.KEY);
		if (isReqRecordOperaType(operaType)){
			DataRow record = getService().getRecord(param);
			this.setAttributes(record);
			param.put("PR_NAME", record.stringValue("PR_NAME"));
			param.put("PR_POST", record.stringValue("PR_POST"));
			param.put("PR_SORT", record.stringValue("PR_SORT"));
		}
		this.setAttributes(param);
		this.setOperaType(operaType);
		processPageAttributes(param);
		return new LocalRenderer(getPage());
	}
    
    public ViewRenderer doSaveAction(DataParam param){
		String operateType = param.get(OperaType.KEY);
		if (OperaType.CREATE.equals(operateType)){
			getService().createRecord(param);	
		}
		else if (OperaType.UPDATE.equals(operateType)){
			getService().updateRecord(param);	
		}
		return new RedirectRenderer(getHandlerURL(listHandlerClass));
	}

    protected void processPageAttributes(DataParam param) {
        setAttribute("PR_SEX",FormSelectFactory.create("USER_SEX").addSelectedValue(getOperaAttributeValue("PR_SEX","M")));
    }

    protected ProjectReportManage getService() {
        return (ProjectReportManage) this.lookupService(this.getServiceId());
    }
}
